﻿using System.Web.Mvc;
using System.Web.Routing;

namespace RESTController
{
    public static class RouteExtensions
    {
        public static void Connect(this RouteCollection routes, string routeName, string path, string controller, string action, params string[] methods)
        {
            routes.MapRoute(routeName, path, new { controller, action, id = UrlParameter.Optional }, new { method = new HttpMethodConstraint(methods) });
        }

        /// <summary>
        /// Create a new resource route that responds to Index, List, New, Edit, and Delete operations.
        /// </summary>
        public static void Resource(this RouteCollection routes, string controller, string basePath)
        {
            routes.MapRoute(controller + "_Index", basePath, new { controller, action = "Index" }, new { method = new HttpMethodConstraint("GET") });
            routes.MapRoute(controller + "_List", basePath + "/list", new { controller, action = "List" }, new { method = new HttpMethodConstraint("GET") });
            routes.MapRoute(controller + "_New", basePath + "/new", new { controller, action = "New" }, new { method = new HttpMethodConstraint("GET", RESTControllerSettings.Settings.NewHttpMethod) });
            routes.MapRoute(controller + "_Count", basePath + "/count", new { controller, action = "Count" }, new { method = new HttpMethodConstraint("GET", "POST") });
            routes.MapRoute(controller + "_Show", basePath + "/{id}", new { controller, action = "Show", id = "" }, new { method = new HttpMethodConstraint("GET") });
            routes.MapRoute(controller + "_Edit", basePath + "/{id}/edit", new { controller, action = "Edit", id = "" }, new { method = new HttpMethodConstraint("GET", RESTControllerSettings.Settings.EditHttpMethod) });
            routes.MapRoute(controller + "_Delete", basePath + "/{id}/delete", new { controller, action = "Delete", id = "" }, new { method = new HttpMethodConstraint("GET", RESTControllerSettings.Settings.DeleteHttpMethod) });
        }

        /// <summary>
        /// Create a new nested resource route that responds to Index, List, New, Edit, and Delete operations.
        /// </summary>
        public static void NestedResource(this RouteCollection routes, string controller, string parentPath, string childPath)
        {
            routes.MapRoute(controller + "_Index", parentPath + "/{parentId}/" + childPath, new { controller, action = "Index" }, new { method = new HttpMethodConstraint("GET") });
            routes.MapRoute(controller + "_List", parentPath + "/{parentId}/" + childPath + "/list", new { controller, action = "List" }, new { method = new HttpMethodConstraint("GET") });
            routes.MapRoute(controller + "_New", parentPath + "/{parentId}/" + childPath + "/new", new { controller, action = "New" }, new { method = new HttpMethodConstraint("GET", RESTControllerSettings.Settings.NewHttpMethod) });
            routes.MapRoute(controller + "_Count", parentPath + "/{parentId}/" + childPath + "/count", new { controller, action = "Count" }, new { method = new HttpMethodConstraint("GET", "POST") });
            routes.MapRoute(controller + "_Show", childPath + "/{id}", new { controller, action = "Show", id = "" }, new { method = new HttpMethodConstraint("GET") });
            routes.MapRoute(controller + "_Edit", childPath + "/{id}/edit", new { controller, action = "Edit", id = "" }, new { method = new HttpMethodConstraint("GET", RESTControllerSettings.Settings.EditHttpMethod) });
            routes.MapRoute(controller + "_Delete", childPath + "/{id}/delete", new { controller, action = "Delete", id = "" }, new { method = new HttpMethodConstraint("GET", RESTControllerSettings.Settings.DeleteHttpMethod) });
        }
    }
}
